package wci.backend;

import wci.intermediate.ICode;
import wci.intermediate.SymTab;
import wci.intermediate.SymTabStack;
import wci.message.*;

/**
 * <h1>Backend</h1>
 *
 * <p>框架后端组件类</p>
 *
 * <p>Copyright (c) 2009 by Ronald Mak</p>
 * <p>For instructional purposes only.  No warranties.</p>
 */
public abstract class Backend implements MessageProducer
{
	// 消息处理代理类
    protected static MessageHandler messageHandler = new MessageHandler();  
    protected SymTabStack symTabStack;  // 符号表堆栈
    protected ICode iCode;    // 中间码即语法树

    public ICode getICode()
    {
        return iCode;
    }
    public SymTabStack getSymTabStack() {
		return symTabStack;
	}
    public MessageHandler getMessageHandler()
    {
        return messageHandler;
    }

    /**
     * 处理来自前端Parser产生的中间码和符号表，或编译生成代码或执行
     * @param iCode 符号表
     * @param stack 符号表堆栈
     * @throws Exception
     */
    public abstract void process(ICode iCode, SymTabStack stack)
        throws Exception;

    public void sendMessage(Message message)
    {
        messageHandler.sendMessage(message);
    }
    public void addMessageListener(MessageListener listener)
    {
        messageHandler.addListener(listener);
    }
    public void removeMessageListener(MessageListener listener)
    {
        messageHandler.removeListener(listener);
    }
}
